﻿using DevComponents.DotNetBar.Metro;
using PlatForm.Controller;
using PlatForm.DataLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlatForm.Presentation
{
    public partial class frmStore : MetroForm
    {
        StoreController storeCtrl = new StoreController();

        public frmStore()
        {
            InitializeComponent();
            dGVStore.AutoGenerateColumns = false;
        }

        private void Init()
        {
            var lstStore = storeCtrl.GetList();
            dGVStore.DataSource = lstStore;

            var suppl_No = new Binding("Text", lstStore, "ST_No");
            txtST_No.DataBindings.Clear();
            txtST_No.DataBindings.Add(suppl_No);

            var suppl_Name = new Binding("Text", lstStore, "ST_Name");
            txtST_Name.DataBindings.Clear();
            txtST_Name.DataBindings.Add(suppl_Name);

            var suppl_Address = new Binding("Text", lstStore, "Address");
            txtAddress.DataBindings.Clear();
            txtAddress.DataBindings.Add(suppl_Address);

            txtST_No.Focus();
        }

        private void frmStore_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void txtST_No_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        public void Delete()
        {
            try
            {
                var msg = MessageBox.Show("Do you want to delete this record?", "Platform", MessageBoxButtons.YesNo);
                if (msg == DialogResult.Yes)
                {
                    storeCtrl.Delete(txtST_No.Text);
                    Init();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not delete this record");
            }
        }

        public void Save()
        {
            var store = new Store()
            {
                ST_No = txtST_No.Text,
                ST_Name = txtST_Name.Text,
                Address = txtAddress.Text
            };
            try
            {
                var x = storeCtrl.Save(store);
                Init();
                if (x == 1)
                    Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not save record");
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                   case Keys.Enter:
                    Save();
                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void Clear()
        {
            txtST_No.Text = "";
            txtST_Name.Text = "";
            txtAddress.Text = "";
            txtST_No.Focus();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Clear();
        }
    }
}
